package com.ruoyi.omchannel.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.*;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;

import com.alibaba.fastjson2.JSON;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.omchannel.helper.QRCodeGenerator;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.omchannel.domain.OmChannel;
import com.ruoyi.omchannel.service.IOmChannelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;

import com.ruoyi.common.config.RuoYiConfig;

/**
 * 渠道管理Controller
 * 
 * @author ningyi
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/om/channel")
public class OmChannelController extends BaseController
{
    //生成 二维码 https://blog.csdn.net/hk000001/article/details/143452271

    //https://blog.csdn.net/qq_38258642/article/details/128961951


    @Autowired
    private IOmChannelService omChannelService;

    @Autowired
    private QRCodeGenerator qrCodeGenerator;


    /**
     * 查询渠道管理列表
     */
    @PreAuthorize("@ss.hasPermi('om:channel:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmChannel omChannel) throws Exception {
        startPage();
        List<OmChannel> list = omChannelService.selectOmChannelList(omChannel);

        // 遍历渠道列表
//        for (OmChannel channel : list) {
            // 使用工具类根据channelUuid生成二维码的base64字符串
            //String base64QRCode = qrCodeGenerator.generateQRCodeAsBase64(channel.getChannelUuid());
//            // 文件
//            String pathFile = qrCodeGenerator.generateQRCodeToFile("cm_"+channel.getChannelUuid());
//
//            // 将生成的base64二维码字符串设置到当前渠道对象的channelImage属性中
//            String  channelPath = RuoYiConfig.getUploadPath()+"/channel/";
//            String pathFIle2 = "/profile/upload/jie/"+"cm_"+channel.getChannelUuid()+".png";


//            String qrCodeBase64 = omChannelService.generateQrCodeZSB(channel);
//            String zsbBase64 = omChannelService.generateQrCodeTYB(channel);
//            channel.setChannelImage(qrCodeBase64);
//            channel.setRemark(zsbBase64);
            // 如果需要更新数据库中的记录，请在此处调用相应的更新方法
            // omChannelService.updateOmChannel(channel);
//        }

        // 这里可以添加其他逻辑，比如打印处理结果等
//        for(OmChannel channel : list) {
//            //System.out.println("Channel UUID: " + channel.getChannelUuid() + ", Image Base64: " + channel.getChannelImage());
//            System.out.println("Channel UUID: " + channel.getChannelUuid() + ", pathFIle2: " + channel.getChannelImage());
//        }

        return getDataTable(list);
    }

    /**
     * 导出渠道管理列表
     */
    @PreAuthorize("@ss.hasPermi('om:channel:export')")
    @Log(title = "渠道管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmChannel omChannel)
    {
        List<OmChannel> list = omChannelService.selectOmChannelList(omChannel);
        ExcelUtil<OmChannel> util = new ExcelUtil<OmChannel>(OmChannel.class);
        util.exportExcel(response, list, "渠道管理数据");
    }

    /**
     * 获取渠道管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('om:channel:query')")
    @GetMapping(value = "/{channelId}")
    public AjaxResult getInfo(@PathVariable("channelId") Long channelId) throws Exception {
        return success(omChannelService.selectOmChannelByChannelId(channelId));
    }

    /**
     * 新增渠道管理
     */
    @PreAuthorize("@ss.hasPermi('om:channel:add')")
    @Log(title = "渠道管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmChannel omChannel)
    {

        String uuid = UUID.randomUUID().toString();
        String shortUuid = uuid.replace("-", "").substring(0, 20);


        //omChannel.setChannelUuid("cm_"+uuid);  千万别 只是生成的时候有
        omChannel.setChannelUuid(shortUuid);
        logger.info("channel add {}", JSON.toJSONString(omChannel));
        return toAjax(omChannelService.insertOmChannel(omChannel));
    }

    /**
     * 修改渠道管理
     */
    @PreAuthorize("@ss.hasPermi('om:channel:edit')")
    @Log(title = "渠道管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmChannel omChannel)
    {
        return toAjax(omChannelService.updateOmChannel(omChannel));
    }

    /**
     * 删除渠道管理
     */
    @PreAuthorize("@ss.hasPermi('om:channel:remove')")
    @Log(title = "渠道管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{channelIds}")
    public AjaxResult remove(@PathVariable Long[] channelIds)
    {
        return toAjax(omChannelService.deleteOmChannelByChannelIds(channelIds));
    }
    @PostMapping("dropList")
    @Anonymous
    public AjaxResult dropList()
    {
        List<OmChannel> list = omChannelService.selectOmChannelList(null);
        return success(list);
    }

}
